<?php

// basecamp
function load_defines( )
{
	define( '___dirs',	'classes, controllers, models, views, helpers', 1 );
	define( '__core',	pathinfo( __FILE__, PATHINFO_DIRNAME ).'/', 1 );
	
	$app = realpath( __core.'../application/' );
	if( !file_exists( $app ) )
		$app = realpath( __core.'../exts/' ); // back-compatibility
	define( '__apps',	$app.'/', 1 );
	
	define( '__site',	realpath( __core.'../site/' ).'/', 1 );
	define( '__exts',	__apps, 1 ); // back-compatibility
	
	define( 'BASEPATH',		__core );
	define( 'APPPATH',		__apps );
	define( 'SITEPATH',		__site );
	define( 'APPLICATION',	__apps );
	
	if( !defined( 'ROOT' ) )
	define( 'root',		pathinfo( $_SERVER['SCRIPT_FILENAME'], PATHINFO_DIRNAME ).'/', 1 );
	
	define( 'ROOTPATH',		ROOT );
	
	// core, exts directories
	foreach( explode( ',', strtolower( ___dirs ) ) as $d )
	{
		$d = trim( $d );
		define( '__'.$d,	__apps.$d.'/', 1 );
		define( '__core_'.$d,	__core.$d.'/', 1 );
	}

	
	define( 'CONSOLE', PHP_SAPI == 'cli' || (!isset($_SERVER['DOCUMENT_ROOT']) && !isset($_SERVER['REQUEST_URI'])) );
	
	define( 'EXT', '.php' );

        /*
         * Temporary helper pre-loaders
         */
        load_helper('form');
        load_helper('url');
        load_helper('nonce');
        load_helper('language');
		load_helper('html');
}


function fetch_file($where, $what, $postfix = '')
{
    $postfix = (array)($postfix);
    $where = (array)($where);
	$what = (array)($what);
	
    foreach($where as $path) {
        foreach($what as $file) {
            foreach($postfix as $p) {
                $filenames = array(
                    $path . $file . $p . EXT,
                    $path . $file
                    );
				
                foreach($filenames as $filename) {
                    if( is_readable($filename) && is_file($filename) )
                        return $filename;
                }
            }
        }
    }
	
    return false;
}

function load_standard_file($name, $types)
{
	$definedTypes = array(
		'class' => array( 'path' => 'classes', 'postfix' => '.class' ),
		'model' => array( 'path' => 'models', 'postfix' => '.class' ),
		'controller' => array( 'path' => 'controllers', 'postfix' => '' ),
		'library' => array( 'path' => 'libraries', 'postfix' => '' ),
		'helper' => array( 'path' => 'helpers', 'postfix' => '_helper' ),
		);
	
	$types = (array)($types);
	foreach( $types as $type )
	{
		if( !isset( $definedTypes[$type] ) )
			throw new Exception( 'unknown file type "'.$type.'"' );
		
		$type = $definedTypes[$type];
		$filename = fetch_file( array(
			APPPATH . $type['path'] . '/',
			BASEPATH . $type['path'] . '/',
			), array( $name, strtolower( $name ) ), $type['postfix'] );
		
	    if($filename !== false)
		{
    	    require_once($filename);
			return true;
		}
	}
}

function load_special_file($file, $path = '/')
{
	$paths = (array)($path);
	$where = array( );

	foreach( $paths as $path )
		$where[] = APPPATH . $path . '/';
	foreach( $paths as $path )
		$where[] = BASEPATH . $path . '/';
	
	$filename = fetch_file( $where, array( $file, strtolower( $file ) ) );
	if($filename !== false)
	{
   	    require_once($filename);
		return true;
	}
}

function load_helper($helper)
{
	return load_standard_file($helper, 'helper');
}


function __autoload( $class )
{
	
	$fullclass = $class.'.class.php';
	
	// controllers
	if( strpos( $fullclass, 'Controller.c' ) != 0 )
	{
		@define( 'yo',1 );
		$fullclass = ( strpos( strtolower($fullclass), 'admincontroller.c' ) != 0 )
			? 'admin/'.str_ireplace( 'AdminController.class.php', '', $fullclass )
			: str_ireplace( 'Controller.class.php', '', $fullclass );
		
		if( load_standard_file( $fullclass, 'controller' ) ) return;
	}
	
	if( load_standard_file( $class, array( 'class', 'model' ) ) ) return;
	
	return @include_once( str_replace( '_', '/', $class.'.php' ) );
}

// for error showing
register_shutdown_function( create_function( '', "singleton::destroy( 'response' );" ) );

// load defines)
load_defines( );


